const RealtimeData = require('../models/RealtimeData');
// models/RealtimeData.js
const Sequelize = require('sequelize');
const { Op } = Sequelize;
const getRealtimeData = async (ctx) => {
  const dataValue = ctx.request.body.data;

  try {
    let data;

    if (dataValue == 0) {
      // 返回前300条数据
      data = await RealtimeData.findAll({
        where: {
          ID: {
            [Op.between]: [1, 300],
          },
        },
      });
    } else if (dataValue >= 1) {
     
      // 返回指定 ID 的数据
      const targetId = 300 + dataValue;
      data = await RealtimeData.findByPk(targetId);
    } else {
      // 其他情况返回前300条数据
      data = await RealtimeData.findAll({
        where: {
          ID: {
            [Op.between]: [1, 300],
          },
        },
      });
    }

    ctx.body = data;
  } catch (error) {
    console.error(error);
    ctx.status = 500;
    ctx.body = { error: 'An error occurred' };
  }
};

module.exports = {
  getRealtimeData,
};
